package cn.edu.njuit.webserver.securityserver.orders;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Description: TODO
 * @Data: 2022/9/19 18:01
 * @Author: guzy
 */
@Configuration
public class OrderQueueConfig {
    /**
     * 定义一个直连交换机，用于消息向4个队列转发
     *
     * @return
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange("exchange.order");
    }

    /**
     * 队列1：待确认订单队列
     *
     * @return
     */
    @Bean
    public Queue OrderConfirmQueue() {
        return new Queue("queue.order.confirm");
    }

    /**
     * 队列2：待配送订单队列：该队列中消息，超过1分钟未被骑手处理，就自动转为商家配送订单
     *
     * @return
     */
    @Bean
    public Queue OrderDeliveQueue() {
        HashMap<String, Object> params = new HashMap<>();
//设置消息到期后用于转发的交换机
        params.put("x-dead-letter-exchange", "exchange.order");
//设置消息到期后消息转发的去向，转去队列3
        params.put("x-dead-letter-routing-key", "shopdelive");
//设置此队列消息有效时长为1分钟
        params.put("x-message-ttl", 60000);
        return new Queue("queue.order.delive", true, false, false, params);
    }

    /**
     * 队列3：无人配送订单队列
     *
     * @return
     */
    @Bean
    public Queue ShopDeliveQueue() {
        return new Queue("queue.shop.delive");
    }

    /**
     * 队列4：配送完成订单队列
     *
     * @return
     */
    @Bean
    public Queue OrderCompleteQueue() {
        return new Queue("queue.order.complete");
    }

    /**
     * 队列1与交换机绑定
     *
     * @return
     */
    @Bean
    public Binding bindingOrderConfirmToOrderExchange() {
        return BindingBuilder.bind(OrderConfirmQueue()).to(orderExchange()).with("confirm");
    }

    /**
     * 队列2与交换机绑定
     *
     * @return
     */
    @Bean
    public Binding bindingOrderDeliveToOrderExchange() {
        return BindingBuilder.bind(OrderDeliveQueue()).to(orderExchange()).with("delive");
    }

    /**
     * 队列3与交换机绑定
     *
     * @return
     */
    @Bean
    public Binding bindingShopDeliveToOrderExchange() {
        return BindingBuilder.bind(ShopDeliveQueue()).to(orderExchange()).with("shopdelive");
    }

    /**
     * 队列4与交换机绑定
     *
     * @return
     */
    @Bean
    public Binding bindingOrderCompleteToOrderExchange() {
        return BindingBuilder.bind(OrderCompleteQueue()).to(orderExchange()).with("complete");
    }
}
